package ro.vikingsoft.androidprj;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class AndroresultgridActivity extends Activity {
    /** Called when the activity is first created. */
	
	private ArrayList<RadioGroup> listOfRadioGroups = null;
    private TextView mTextField = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        TableLayout table = new TableLayout(this);  
        table.setBackgroundColor(Color.DKGRAY);
        table.setStretchAllColumns(true);  
        table.setShrinkAllColumns(true);  
      
        TableRow rowTitle = new TableRow(this);  
        rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);  
      
        // title column/row  
        TextView title = new TextView(this);  
        title.setText("Results Table");  
      
        title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);  
        title.setGravity(Gravity.CENTER);  
        title.setTypeface(Typeface.SERIF, Typeface.BOLD);  
      
        TableRow.LayoutParams params = new TableRow.LayoutParams();  
        params.span = 7;  
      
        rowTitle.addView(title, params);  
      
        
        TableRow rowAnswerLabels = new TableRow(this);
        TextView empty = new TextView(this);
        //rowAnswerLabels.addView(empty);
        
        DigitalClock dgClock = new DigitalClock(this);
        rowAnswerLabels.addView(dgClock);	
        mTextField = new TextView(this);
        rowAnswerLabels.addView(mTextField);
        rowAnswerLabels.setBackgroundColor(Color.WHITE);
        rowAnswerLabels.setHorizontalGravity(Gravity.LEFT);
        
//        TextView resLabel = null;
//        for (int i=1;i<=5;i++){
//        	resLabel = new TextView(this);  
//        	resLabel.setText("Res "+i);  
//        	resLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
//        	resLabel.setGravity(Gravity.CENTER_HORIZONTAL);
//            rowAnswerLabels.addView(resLabel);
//        }
      
        table.addView(rowTitle);  
        table.addView(rowAnswerLabels);
        listOfRadioGroups = new ArrayList<RadioGroup>();
        
        for (int i=1;i<=5;i++){
        	TableRow rowQuestions = new TableRow(this);
        	rowQuestions.setGravity(Gravity.CENTER_VERTICAL);
        	
        	TextView rowQuestionsLabel = new TextView(this);  
        	rowQuestionsLabel.setText("Question "+i);  
        	rowQuestionsLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
        	rowQuestionsLabel.setGravity(Gravity.CENTER_VERTICAL);
        	rowQuestions.addView(rowQuestionsLabel);
        	
        	RadioGroup rGroup = new RadioGroup(this);
        	 
        	
        	rGroup.setOrientation(0);
        	for (int ii=1;ii<=5;ii++){
        		OnClickListener radio_listener = new OnClickListener() {
        	        public void onClick(View v) {
        	            onRadioButtonClick(v);
        	        }
        	    };
        		
        		RadioButton rbutton = new RadioButton(this);
        		rbutton.setEnabled(false);
                rbutton.setOnClickListener(radio_listener);     
                rGroup.addView(rbutton);
                
        	}
        	
        	listOfRadioGroups.add((RadioGroup)rGroup);
        	rowQuestions.addView(rGroup, params);
        	
        	table.addView(rowQuestions);
        };
        
        //Add buttons
        OnClickListener btnStart_listener = new OnClickListener() {
	        public void onClick(View v) {
	        	Button btn = (Button)v;
	        	if (btn.getText()!="DONE"){
	        		btn.setText("DONE");
	        		
	        	}
	        	else{
	        		btn.setText("START");
	        		
	        	}
	        		
	        	onStartButtonClick();
	        }
	    };
        TableRow rowButtons = new TableRow(this);
        Button startButton = new Button(this);
        startButton.setOnClickListener(btnStart_listener);
        startButton.setText("START");
        rowButtons.addView(startButton, params);
        table.addView(rowButtons);
        
	    
        setContentView(table);  
           
    }
    
    public void onRadioButtonClick(View v) {
        RadioButton button = (RadioButton) v;
        Toast.makeText(AndroresultgridActivity.this,
            button.getText() + " was chosen.",
            Toast.LENGTH_SHORT).show();
    }
    
    public void onStartButtonClick() {
    	
    	changeRGActions(listOfRadioGroups);
    	
    	new CountDownTimer(30000, 1000) {

    	     public void onTick(long millisUntilFinished) {
    	         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    	     }

    	     public void onFinish() {
    	         mTextField.setText("done!");
    	     }
    	}.start();
    	
    }
    
    private ArrayList<RadioButton> getRadioButtonsOf(RadioGroup rg){
    	int count = rg.getChildCount();
        ArrayList<RadioButton> listOfRadioButtons = new ArrayList<RadioButton>();
        for (int i=0;i<count;i++) {
            View o = rg.getChildAt(i);
            if (o instanceof RadioButton) {
                listOfRadioButtons.add((RadioButton)o);
            }
        }
        
        return listOfRadioButtons;
    }
    
    private void setRadioButtonsDisable(RadioGroup rg){
    	int count = rg.getChildCount();
        for (int i=0;i<count;i++) {
            View o = rg.getChildAt(i);
            if (o instanceof RadioButton) {
            	if (o.isEnabled())
            		o.setEnabled(false);
            	else
            		o.setEnabled(true);
            }
        }    
    }
    
    private void changeRGActions(ArrayList<RadioGroup> rg){
    	
    	for (int i=0;i<rg.size();i++) {
    		setRadioButtonsDisable(rg.get(i));
    	}

    }

}